Passed
Pull Request — master (#2)
by Khalil
01:10
created

exports.client   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
dl 0
loc 4
rs 10
nop 2
1
/**
2
 * @author Jinzulen
3
 * @license Apache 2.0
4
 * 
5
 * TenorJS - Lightweight NodeJS wrapper around the Tenor.com API.
6
 */
7
const FS     = require("fs"),
8
      Colors = require("colors");
9
10
exports.client = function (Credentials)
11
{
12
      const Filters      = ["off", "low", "medium", "high"],
13
            MediaFilters = ["basic", "minimal"];
14
15
      /**
16
       * Credentials checks.
17
       */
18
      if (!Credentials.Key || !Credentials.Locale || !Credentials.Filter)
19
      {
20
            throw new Error ("Client configuration is not complete; please ensure all configuration parameters are satisfied (Key, Locale, Filter).");
21
      }
22
      
23
      if (!Filters.includes(Credentials.Filter.toLowerCase()))
24
      {
25
            throw new Error ("Content filter level has to be one of these options: off, low, medium, high.");
26
      }
27
28
      if (Credentials.MediaFilter)
29
      {
30
            if (!MediaFilters.includes(Credentials.MediaFilter.toLowerCase()))
31
            {
32
                  throw new Error ("Media content filter has to be one of these options: basic, minimal.");
33
            }
34
      } else if (!Credentials.MediaFilter) {
35
            Credentials.MediaFilter = "minimal"; 
36
      }
37
38
      if (!Credentials.DateFormat) Credentials.DateFormat = "D/MM/YYYY - H:mm:ss A";
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
39
40
      Credentials.Gate   = "https://api.tenor.com/v1";
41
      Credentials.Filter = Credentials.Filter.toLowerCase();
42
43
      /**
44
       * Should probably move this elsewhere in the future, not a good
45
       * idea having this much code and heavy-lifting in one place.
46
       */
47
      let Creds = JSON.stringify(Credentials);
48
      
49
      function writeConfig()
50
      {
51
            FS.writeFileSync("tenor_config.json", Creds, function (Error) {
52
                  if (Error) throw Error;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
                  console.log(Colors.bold.green(`# [TenorJS] Changes have been made to the configuration file. Process should be restarted.`));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
54
                  process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
55
            });
56
      }
57
      
58
      try
59
      {
60
            if (!FS.existsSync("tenor_config.json"))
61
            {
62
                  writeConfig();
63
            } else {
64
                  FS.readFile("tenor_config.json", "utf8", function (Error, Data) {
65
                        if (Error) throw Error;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
66
                        if (Data !== Creds) writeConfig();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
67
                  });
68
            }
69
      } catch (E) {
70
            throw E;
71
      }
72
73
      return require("./src")(Credentials);
74
};